package com.ssm.zhihu_ssm.service.impl;


import com.ssm.zhihu_ssm.dao.LetterDao;
import com.ssm.zhihu_ssm.dao.UserDAO;
import com.ssm.zhihu_ssm.entity.Letter;
import com.ssm.zhihu_ssm.entity.LetterReturn;
import com.ssm.zhihu_ssm.entity.User;
import com.ssm.zhihu_ssm.service.LetterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "letterService")
public class LetterServiceImpl implements LetterService {

    @Autowired
    private LetterDao letterDao;
    @Autowired
    private UserDAO userDAO;

    @Override
    public List<LetterReturn> findLetterReturn(int id) {

        List<LetterReturn>  letters= letterDao.findLetterReturn(id);
        System.out.println(letters.size());
        //删除自己发给同一个人的
        for(int  i = 0;i<letters.size();i++)
        {
            for (int j = i+1 ;j<letters.size();j++)
            {
                if (letters.get(i).getUid() == letters.get(j).getUid()&&
                        letters.get(i).getFromID()==letters.get(j).getFromID()){
                    letters.remove(j);
                    j--;

                }
            }
        }
        System.out.println(letters.size());
        //删除两个人互相发的
        for(int  i = 0;i<letters.size();i++)
        {
            for (int j = i+1 ;j<letters.size();j++)
            {
                if (letters.get(i).getFromID()==letters.get(j).getUid()&&
                        letters.get(i).getUid()==letters.get(j).getFromID()
                ){
                    System.out.println(letters.get(j).getUid());
                    letters.remove(j);

                }
            }
        }
        for(int  i = 0;i<letters.size();i++)
        {
            if (letters.get(i).getUid()==id){
                int x =letters.get(i).getFromID();
                letters.get(i).setOtherid(x);
            }
            else {
                int x =letters.get(i).getUid();
                letters.get(i).setOtherid(x);
            }
        }
        for(int  i = 0;i<letters.size();i++)
        {
            int x = letters.get(i).getOtherid();
             User user =  userDAO.findUserMessage(x);
             letters.get(i).setNickname(user.getUserMessages().getNickName());
        }

        return letters;
    }

    @Override
    public List<Letter> findLetterWith(int id,int fromId) {
        List<Letter>  letters= letterDao.findLetterWith(id,fromId);
        return letters;
    }

    @Override
    public boolean delectLetter(int id) {
        Letter letter = letterDao.findLettersID(id);
        if (letter != null)
        {
            letterDao.delectLetters(id);
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean addletters(int uid, int from_id, String content) {
        Letter letter = new Letter();
        letter.setUid(uid);
        letter.setContent(content);
        letter.setFromID(from_id);
        letterDao.addLetter(letter);
        return true;
    }
}
